﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace weighing
{
    /// <summary>
    /// InWeighingSettingUc.xaml 入库过磅设置的交互逻辑
    /// </summary>
    public partial class InWeighingSettingUc : System.Windows.Controls.UserControl
    {
        #region variables

        private bool isSetDefaultValue = false;
        #endregion

        public InWeighingSettingUc()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetDefaultValue();
        }

        private void SetDefaultValue()
        {
            isSetDefaultValue = true;
            // in
            this.inNecessarySendWeighingCb.IsChecked = App.Config.WeighingConfig.InNecessarySendWeighing;
            this.InNecessaryReceiveCb.IsChecked = App.Config.WeighingConfig.InNecessaryReceive;
            this.InLockReceiveCb.IsChecked = App.Config.WeighingConfig.InBindingReceive;
            this.InNecessarySendCb.IsChecked = App.Config.WeighingConfig.InNecessarySend;
            this.InCarNumberCb.IsChecked = App.Config.WeighingConfig.InCheckCarNumber;
            //times
            if (App.Config.WeighingConfig.Times == 2)
            {
                this.TwiceRb.IsChecked = true;
            }
            else if (App.Config.WeighingConfig.Times == 1)
            {
                this.OnceRb.IsChecked = true;
            }
            else
            {
                this.TwiceRb.IsChecked = true;
            }
            //this.InNecessaryReceiveYardCb.IsChecked = App.Config.WeighingConfig.InNecessaryReceiveYard;
            //this.InNecessarySendYardCb.IsChecked = App.Config.WeighingConfig.InNecessarySendYard;
            this.InTranceCompanyCb.IsChecked = App.Config.WeighingConfig.InNecessaryTranceCompany;

            this.ignoreTraeWeightCb.IsChecked = App.Config.WeighingConfig.InIgnoreTraeWeighing;
            this.InCarNumberNeedCb.IsChecked = App.Config.WeighingConfig.InNecessaryCar;
            this.InNotSameCompanyCb.IsChecked = App.Config.WeighingConfig.InNotSameCompany;
            this.InNecessaryMaterialCb.IsChecked = App.Config.WeighingConfig.InNecessaryMaterial;
            isSetDefaultValue = false;
        }

        #region In 入库过磅

        //入库过磅 原发净重 是否为必须
        private void InNecessarySendWeighingCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.InNecessarySendWeighing != this.inNecessarySendWeighingCb.IsChecked)
            {
                App.Config.WeighingConfig.InNecessarySendWeighing = (bool)this.inNecessarySendWeighingCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }

        //入库过磅 收货方信息是否为必须
        private void InNecessaryReceiveCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.InNecessaryReceive != this.InNecessaryReceiveCb.IsChecked)
            {
                App.Config.WeighingConfig.InNecessaryReceive = (bool)this.InNecessaryReceiveCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        //入库过磅是否绑定收货方为使用单位
        private void InLockReceiveCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.InBindingReceive != this.InLockReceiveCb.IsChecked)
            {
                App.Config.WeighingConfig.InBindingReceive = (bool)this.InLockReceiveCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }

        //入库过磅 发货信息是否为界必须
        private void InNecessarySendCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.InNecessarySend != this.InNecessarySendCb.IsChecked)
            {
                App.Config.WeighingConfig.InNecessarySend = (bool)this.InNecessarySendCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        #endregion

        #region 次数设置
        private void OnceRb_Checked(object sender, RoutedEventArgs e)
        {           
            if (App.Config.WeighingConfig.Times == 1) return;
            App.Config.WeighingConfig.Times = 1;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void TwiceRb_Checked(object sender, RoutedEventArgs e)
        {            
            if (App.Config.WeighingConfig.Times == 2) return;
            App.Config.WeighingConfig.Times = 2;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 车牌号必须合法

        private void InCarNumberCb_changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.InCheckCarNumber != this.InCarNumberCb.IsChecked)
            {
                App.Config.WeighingConfig.InCheckCarNumber = (bool)this.InCarNumberCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        #endregion


        #region 发货公司地址必须
  
        private void InNecessarySendYardCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessarySendYard = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        private void InNecessarySendYardCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessarySendYard = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 收货公司地址必须

      
        private void InNecessaryReceiveYardCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryReceiveYard = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void InNecessaryReceiveYardCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryReceiveYard = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 运输公司地址必须

        private void InTranceCompanyCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryTranceCompany = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void InTranceCompanyCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryTranceCompany = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 不计皮重

        private void IgnoreTraeWeightCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InIgnoreTraeWeighing = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            DelegeteFactory.Instance.OnWeighingConfigChanged?.Invoke(App.Config.WeighingConfig);
        }

        private void IgnoreTraeWeightCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InIgnoreTraeWeighing = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            DelegeteFactory.Instance.OnWeighingConfigChanged?.Invoke(App.Config.WeighingConfig);
        }

       
        #endregion

        #region 车牌号必须

        private void InCarNumberNeedCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryCar = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void InCarNumberNeedCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryCar = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 收发公司不能相同
        private void InNotSameCompanyCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNotSameCompany = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void InNotSameCompanyCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNotSameCompany = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 货物信息必须
        private void InMaterialCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryMaterial = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void InMaterialCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.InNecessaryMaterial = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion


        /// <summary>
        /// 设置备用字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackupFieldBtn_Click(object sender, RoutedEventArgs e)
        {
            new AddBackupFieldWin()
            {
                BillType = WeightingBillType.Buy,
                Owner = App.Current.MainWindow,
            }.Show();
        }
    }
}
